<?php 
namespace app\common\controller;
use think\Controller;
use think\Db;
use app\admin\model\Goods_category as categoryModel;
use app\admin\model\Goods as goodsModel;
use app\admin\model\Pictures as picsModel;
/**
* 用来提取公共数据的
*/
class Base extends Controller
{	

	function __construct(){
		$this->web_title="直大购网站";
		// $this->col_title=self::categoryName();
        $this->categoryRes=self::categoryList();
    }
	/**
	* 取得栏目名字
	* return string
	*/
	public function categoryName($id=''){
		$res=Db::table('goods_category')->field('id,pid,cname')->where('id',$id)->select();
		//返回栏目名字string
		return $res[0];
	}
	/**
	* 取得栏目列表
	* return array
	*/
	public function categoryList(){
		$tableGoodsArr=new categoryModel;
		return $tableGoodsArr->getmenu();
	}
	/**
	* 取得子栏目列表
	* return array	
	*/
	public function getChildCategoryList($id=''){
		if ($id) {
			$tableGoodsArr=new categoryModel;
			$rows = $tableGoodsArr->getmenu($id);
			// $rows = Db::table('goods_category_copy')->field('id,pid,cname,path')->select();
			// foreach ($rows as &$row) {
			// 	$num=substr_count($row['path'],'-');
			// 	$pre=str_repeat('——',$num);
			// 	$row['tree']=$pre.$row['cname'];
			// }
			// foreach ($rows as $row2) {
			// 	$arr[]=$row2['path'].'-'.$row2['id'];
			// }
			// array_multisort($arr,$rows);
			// foreach($rows as $row3){
			// 	echo "<p>{$row3['id']}{$row3['tree']}</p>";
			// }
		}
		return $rows;		
	}
	/**
	* 取得栏目下的商品
	* return array
	*/
	public function getGoodsList($catid='',$g_order=''){
		if($g_order){
			// $res=new goodsModel;
			$list = goodsModel::where('cat_id',$catid)->order($g_order,'desc')->select();
			return $list;
		}else{
			$list = goodsModel::where('cat_id',$catid)->select();
			return $list;
		}
	}
	/**
	* 获取商品img数据
	* return array
	*/
	public function getGoodsImgList($gid=""){
		$imgList=picsModel::all(['album_id'=>$gid]);
		return $imgList;
	}
	public function getChildGoods($catid=""){
		$tableGoodsArr=new categoryModel;
		$level=$tableGoodsArr->field('level')->where('id',$catid)->find();
		$ss=$level['level'];
		$childArr=$tableGoodsArr->where('pid',$catid)->select();
		/**循环方式获取该栏目下的子栏目id
		***
		**/
		foreach ($childArr as $row) {
			if($row['pid']==$catid){
				$pidArr[]=$row['id'];
			}else{
				$pidArr=null;
			}
		}
		//获取path
		$pathArr=Db::table('goods_category')->field('id,path')->select();
		foreach ($pathArr as $value) {
			$num=substr_count($value['path'],'_');
			if($num>1){
				$arr=explode('_',$value['path']);
				if($arr[$ss]==$catid){
					$childArrId[]=$value['id'];
				}
			}else{
				$childArrId=null;
			}
		}
		return $childArrId;
	}
	public function getChildCatId($catid=""){
		$tableGoodsArr=new categoryModel;
		//查询该栏目下的子目录
		if ($childArr = $tableGoodsArr->getmenu($catid)) {
			//循环遍历数组内id下是否还有子目录
			foreach ($childArr as $childVal) {
				//进行对子目录再遍历
				if($child = $tableGoodsArr->getmenu($childVal['id'])){
					foreach ($child as $val) {
						$catnum[]=$val['id'];
					}
				}else{
					$catnum[]=$childVal['id'];
				}
				
			}
			return $catnum;
		}else{
			return $catnum=array();
		}
		
	}
	/**
	* 取得栏目最终的pid
	* return array
	*/
	public function getLastPid($catid,&$lst=''){
		$pid=Db::table('goods_category')->field('id,pid')->where('id',$catid)->select();
		//遍历
		foreach($pid as $val){
			if($val['pid']==0){
				$lst=$val['id'];
			}else{
				$this->getLastPid($val['pid'],$lst);
			}
		}
		return $lst;
	}
	public function getPidToChild($id){
		$tableGoodsArr=new categoryModel;
		//查询该栏目下的子目录
		$childArr = $tableGoodsArr->getChildCate($id);
		return $childArr;
	}
	/**
	* 取得栏目地址导航 函数
	* return urllist
	*/
    public function place($id=''){
        if (isset($id)) {
            $place=$this->findParent($id);
        }else{
            $place='没有参数';
        }
        krsort($place);
        $placeurl='';
        foreach($place as $vals){
            $placeurl.= $vals;
        }
        return $placeurl;
    }
    private function findParent($id="",&$parent=array()){
        $arr = Db::table('goods_category')->field('id,pid,cname')->where('id',$id)->select();
        foreach($arr as $val){
            if($val['pid']==0){
                $parent[]="<li><a href='goodslist?catid={$val['id']}'>{$val['cname']}</a></li>";
            }else{
                $parent[]="<li><a href='goodslist?catid={$val['id']}'>{$val['cname']}</a></li>";
                $this->findParent($val['pid'],$parent);
            }
        }
        return $parent;
    }
    /**
	* 根据商品gid取得栏目cid
	* 
	*/
	public function getGidToCid($gid=''){
		if($gid){
			$arr = goodsModel::get($gid);
		}
		return $arr['cat_id'];
	}
	/**
	* 根据商品gid取得商品信息
	* return Array
	*/
	public function getGoodsInfo($gid=''){
		if($gid){
			$arr=goodsModel::get($gid);
		}
		return $arr;
	}
}
